package com.arcsoft.commonbase.base

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.arcsoft.commonbase.R
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_finish_load
import com.arcsoft.commonbase.databinding.CustomToolbarBinding
import com.arcsoft.commonbase.ext.showLoadingDialog
import com.arcsoft.commonbase.widgets.ProgressDialogVer
import com.blankj.utilcode.util.ActivityUtils
import com.dylanc.viewbinding.base.ViewBindingUtil
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

abstract class BaseBindingFragment<VB : ViewBinding> : Fragment() {

    var isLoadNetFinish: Boolean = false
    lateinit var binding: VB

    var lazyLoaded = false
    lateinit var mActivity: Activity

    private var loadingView: ProgressDialogVer? = null

    //刷新布局
    var baseSmartRefreshLayout: SmartRefreshLayout? = null

    //当前页数
    var currentPage: Int = 1
    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as Activity
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = ViewBindingUtil.inflateWithGeneric(this, inflater, container, false)
        onCreateViewBind(savedInstanceState)
        observe()
        return binding.root
    }

    open fun onCreateViewBind(savedInstanceState: Bundle?) {}


    override fun onResume() {
        super.onResume()
        // 实现懒加载
        if (!lazyLoaded) {
            lazyLoadData()
            loadNetData()
            lazyLoaded = true
        }
    }

    /**
     * 懒加载数据
     */
    abstract fun lazyLoadData()

    /**
     * 防止内存泄漏
     */
    override fun onDestroyView() {
        super.onDestroyView()
//        binding = null
    }


    fun showLoading(cancelable: Boolean = true) {
        loadingView?.takeIf { it.isShowing }?.let {
            // 已经在loading 不重复展示
            return
        }
        context?.let {
            loadingView = showLoadingDialog(it, cancelable)
        }
    }

    fun hideLoading() {
        loadingView?.dismiss()
        loadingView = null
    }


    open fun loadNetData() {

    }

    /**
     * bus监听
     */
    open fun observe() {
        Bus.observe<String>(bus_finish_load, this) {
            finishRefreshAndLoadMore()
        }
    }

    /**
     * 刷新和加载更多
     */
    open fun setOnRefreshLoadMore(baseSmartRefreshLayout: SmartRefreshLayout?) {
        this.baseSmartRefreshLayout = baseSmartRefreshLayout
        baseSmartRefreshLayout?.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                currentPage++
                loadNetData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                currentPage = 1
                loadNetData()
            }
        })
    }

    /*** 刷新和加载更多完成*/
    fun finishRefreshAndLoadMore() {
        baseSmartRefreshLayout?.finishRefresh()
        baseSmartRefreshLayout?.finishLoadMore()
    }

    /**
     *设置中心title和返回监听
     */
    open fun CustomToolbarBinding.setTitleText(
        title: String?, textVisible: Boolean = false, bgTransparent: Boolean = false, rightTitle: String = "", click: (() -> Unit)? = null
    ) {
        tvCenterTitle.text = title
        btnBack.setOnClickListener {

            ActivityUtils.getTopActivity().finish() }

        backText.visibility = if (textVisible) View.VISIBLE else View.INVISIBLE
        tvRightTitle.text = rightTitle
        tvRightTitle.setOnClickListener { click?.invoke() }
        if (bgTransparent) {
            toolbarLayout.setBackgroundColor(Color.TRANSPARENT)
        }

    }
}